package com.sunday.common.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;





/**
 * Created by 刘涛 on 2017/8/27.
 */

abstract public class MyWindow extends PopupWindow{
    private Activity activity;
    MyWindow window;
    public View.OnClickListener onClickListener;
    public MyWindow(Context context){
        window = this;
        activity = (Activity)context;
        ColorDrawable cd = new ColorDrawable(0x000000);
        setBackgroundDrawable(cd);
        setOutsideTouchable(true);
        setFocusable(true);
        //setAnimationStyle(R.style.popuwindow);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1f;
                activity.getWindow().setAttributes(lp);
            }
        });

    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }


    public void show(View view){
        if(window.isShowing()){
            return ;
        }
        //产生背景变暗效果
        WindowManager.LayoutParams lp= activity.getWindow().getAttributes();
        lp.alpha = 0.4f;
        activity.getWindow().setAttributes(lp);
        window.showAtLocation(view,
                Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL, 0, 0);
    }


}
